// pages/apply/choosecourselist/choosecourselist.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require('../../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        typeall: 3,
        keyword: '',
        page: 0,
        listarr: [],
        typearr: [],
        errcode: -1,
        msg: '',
        isjfyc: false,
        bkitem: '',
        is_team: 1, //是否点击校区
        is_cate1: 1, //是否点击分类筛选
        is_cate2: 1, //是否点击专业筛选
        is_cj: 1, //是否点击成交人筛选
        statusarr: [{ title: '全部', id: 0 }, { title: '本人成交', id: 2}, { title: '非本人成交', id: 1 }],
        catearr: [{ id: 0, title: '全部' }, { id: 1, title: '技能' }, { id: 2, title: '理论' }, { id: 3, title: '全程' }],
        teamarr: '',
        is_all_show: 1,
        team_acc_name_arr: [], //选择的校区名称
        team_acc_name: '',
        team_id: [''], //选择的校区id
        team_acc_id: '',
        cjname: '本人成交',
        cjname1: '本人成交',
        cjid1:2, //成交id
        type:0, //type 162-技能提升,163-中级,164-执业医师
        is_type1: '',
        is_type: '',
        is_cate3: '',
        is_cate: '',
        is_cate_name: '',
        is_cate_name1: '',
        is_type_name: '',
        is_type_name1: '',
        is_dl:0,
    },
    cloasebukuan(){
        this.setData({
            isjfyc:false
        })
    },
    searchclick(e){
        var that = this;
        var keyword = e.detail.value.keykown;
        that.setData({
            keyword: keyword,
        })
        that.consCourses();
    },
    // 选择专业
    cateChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            is_cate3: item.id,
            is_cate_name1: item.title
        })
    },
    paymentChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            is_type1: item.id,
            is_type_name1: item.title
        })
    },
    zyzlclick(e) {
        var that = this;
        var type = e.currentTarget.dataset.type;
        that.setData({
            type: type,
            is_type1: '',
            is_type: '',
            is_cate3: '',
            is_cate: '',
            is_cate_name: '',
            is_cate_name1: '',
            is_type_name: '',
            is_type_name1: '',
            page: 0
        })
        if (Number(type) == 162) {
            that.jntsCate();
        }
        that.consCourses();
    },
    // /点击校区
    teamclick() {
        var that = this;
        var is_team = that.data.is_team;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_team: is_team1,
            is_all_show: is_all_show,
            is_cate1: 1, //是否点击分类筛选
            is_cate2: 1, //是否点击专业筛选
            is_cj: 1, //是否点击成交人筛选
        })
    },
    statusChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            cjname1: item.title,
            cjid1: item.id
        })
    },
    statusclick() {
        var that = this;
        var is_cj = that.data.is_cj;
        if (Number(is_cj) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_team: 1,
            is_all_show: is_all_show,
            is_cate1: 1, //是否点击分类筛选
            is_cate2: 1, //是否点击专业筛选
            is_cj: is_team1, //是否点击成交人筛选
        })
    },
    cateclick() {
        var that = this;
        var is_cate1 = that.data.is_cate1;
        if (Number(is_cate1) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_team: 1,
            is_all_show: is_all_show,
            is_cate1: is_team1, //是否点击课程筛选
            is_cate2: 1, //是否点击专业筛选
            is_cj: 1, //是否点击成交人筛选
        })
    },
    zyclick() {
        var that = this;
        var is_cate2 = that.data.is_cate2;
        if (Number(is_cate2) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_team: 1,
            is_all_show: is_all_show,
            is_cate2: is_team1, //是否点击课程筛选
            is_cate1: 1, //是否点击专业筛选
            is_cj: 1, //是否点击成交人筛选
        })
    },
    // 选择校区
    teamChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        if (item.id == '') {
            var team_id = [''];
            var team_acc_name_arr = ['全部'];
        } else {
            var team_id = that.data.team_id;
            var team_acc_name_arr = that.data.team_acc_name_arr;
            if (team_id.indexOf(item.id) >= 0) {
                var index = team_id.indexOf(item.id);
                var index1 = team_acc_name_arr.indexOf(item.title);
                team_id.splice(index, 1);
                team_acc_name_arr.splice(index1, 1);
            } else {
                team_id.push(item.id);
                team_acc_name_arr.push(item.title);
            }
            if (team_id[0] == '') {
                team_id.splice(0, 1);
            }
        }
        that.setData({
            team_id: team_id,
            team_acc_name_arr: team_acc_name_arr,
        })
    },
    closeclcikll() {
        var that = this;
        that.setData({
            is_team: 1, //是否点击校区
            is_cate1: 1, //是否点击分类筛选
            is_cate2: 1, //是否点击专业筛选
            is_cj: 1, //是否点击成交人筛选
            is_all_show: 1,
        })
    },
    // 点击重置
    closech() {
        var that = this;
        that.setData({
            is_team: 1, //是否点击校区
            is_cate1: 1, //是否点击分类筛选
            is_cate2: 1, //是否点击专业筛选
            is_type1: '',
            is_type: '',
            is_cate3: '',
            is_cate: '',
            is_cate_name: '',
            is_cate_name1: '',
            is_type_name: '',
            is_type_name1: '',
            is_cj: 1, //是否点击成交人筛选
            is_all_show: 1,
            team_acc_name_arr: [], //选择的校区名称
            team_acc_name: '',
            team_id: [], //选择的校区id
            cjname: '本人成交',
            cjname1: '本人成交',
            cjid1:2, //成交id
            page: 0,
            team_acc_id:'',
            type:0,
        })
        that.consCourses();
    },

    // 点击确定
    sureclick() {
        var that = this;
        // console.log(that.data.is_type_name1)
        that.setData({
            team_acc_id: that.data.team_id.join(','),
            team_acc_name: that.data.team_acc_name_arr.join(','),
            is_team: 1, //是否点击校区
            is_cate1: 1, //是否点击分类筛选
            is_cate2: 1, //是否点击专业筛选
            is_type: that.data.is_type1,
            is_type_name: that.data.is_type_name1,
            is_cate: that.data.is_cate3,
            is_cate_name: that.data.is_cate_name1,
            is_cj: 1, //是否点击成交人筛选
            is_all_show: 1,
            cjname: that.data.cjname1,
            page: 0,
        })
        that.consCourses();
    },

    bukuanclick(e) {
        var that = this;
        var item = e.target.dataset.item || e.currentTarget.dataset.item;
        that.setData({
            bkitem: item
        })
        if (Number(item.ucp_id > 0)) {
            // 存在异常缴费
            that.setData({
                isjfyc: true
            })
        } else {
            wx.navigateTo({
                url: '/pages/staff/bukuan?course_id=' + item.id + ''
            })
        }
    },

    // 取消订单
    noclick() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        var obj = {
            ucp_id: that.data.bkitem.ucp_id
        }
        if (Number(app.globalData.identity) == 3) {
            var url = 'crmapi/Learn/orderUndo';
        }else{
            var url = 'crmapi/user/orderUndo';
        }
        share.wxRequest(url,obj,function(res){
            wx.hideLoading();
            wx.showToast({
                title: res.data.msg,
                icon: 'none',
            })
            if (Number(res.data.errcode) == 0) {
                that.setData({
                    isjfyc: false
                })
                that.consCourses();
            }
        })
    },

    // 继续缴费
    yesclick() {
        var that = this;
        var item = that.data.bkitem;
        that.setData({
            isjfyc:false,
        })
        // console.log(item);
        // return;

        // pages/staff/choose_course
        wx.navigateTo({
            url: '/pages/staff/choose_course?user_id=-1&tel=' + item.phone + '&name=' + item.user_name + '&ucp_id=' + item.ucp_id + '&newconid='
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var type = options.type; //1退费 2换课 3全部课程
        that.setData({
            typeall: type,
            is_dl: Number(app.globalData.is_dl)
        })
        wx.setStorageSync('coursetype', type)
        if (Number(type) == 3) {
            wx.setNavigationBarTitle({
                title: '选课列表',
            })
        }
        that.teams();
    },

    consCourses() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        var type = that.data.typeall;

        var consultant_id = app.globalData.admin_id;
        var page = that.data.page;
        var keyword = that.data.keyword;
        wx.setStorageSync('keylistphone', keyword);
        if (Number(type) == 3) {
            var pid  = '';
            if(Number(that.data.type)<=0){
                var pid  = '';
            }else if(Number(that.data.type)==163){
                var pid  = 163;
            }else if(Number(that.data.type)==162){
                if(that.data.is_type1 == ''){
                    var pid  = 162;
                }else{
                    var pid  = that.data.is_type1;
                }
            }else if(Number(that.data.type)==164){
                if(that.data.is_cate3 == ''){
                    var pid  = 164;
                }else{
                    var pid  = that.data.is_cate3;
                }
            }
            var obj = {
                consultant_id: consultant_id,
                page: page,
                keyword: keyword,
                area_id:that.data.team_acc_id,
                is_am:that.data.cjid1,
                pid:pid,
            }
            if (Number(app.globalData.identity) == 3) {
                var url = 'crmapi/Learn/allUserCourse'
            } else {
                var url = 'crmapi/user/allUserCourse'
            }
        }
        share.wxRequest(url, obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var newslist = data;
                if (Number(page) == 0) {
                    var lists = newslist;
                } else {
                    var lists = that.data.listarr.concat(newslist);
                }

                if (Number(lists.length) == 0) {
                    var msg = '~暂无数据~'
                } else {
                    var msg = res.data.msg
                }
                that.setData({
                    errcode: res.data.errcode,
                    msg: msg,
                    listarr: lists,
                })
            } else {
                that.setData({
                    errcode: 1002,
                    msg: res.data.msg,
                    listarr: [],
                })
            }
            wx.hideLoading();
        })
    },

    // 技能提升下的二级分类
    jntsCate() {
        var that = this;
        share.wxRequest('crmapi/user/jntsCate', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.lists;
                var obj = [{
                    id: '',
                    title: '全部',
                }]
                that.setData({
                    typearr: obj.concat(data)
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        var type = wx.getStorageSync('coursetype');
        that.setData({
            typeall: type
        })
        if (wx.getStorageSync('keylistphone')) {
            that.setData({
                keyword: wx.getStorageSync('keylistphone')
            })
        } else {
            that.setData({
                keyword: ''
            })
        }
        that.consCourses();
    },

    // 获取校区
    teams() {
        var that = this;
        share.wxRequest('crmapi/User/team', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{
                    id: '',
                    title: '全部',
                }]
                that.setData({
                    teamarr: obj.concat(data)
                })
            }
        })
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        var page = Number(that.data.page) + 1;
        that.setData({
            page: page,
        })
        that.consCourses();
    },
})